package bit;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author ZJX
 * @version 1.0
 * @descption
 * @since 2025/11/19 19:08
 */
public class Day28 {

//    public static void main(String[] args) throws IOException {
//        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
//        PrintWriter out = new PrintWriter(System.out);
//        in.nextToken();
//        int q = (int) in.nval; // 询问次数
//        while (q-- > 0) {
//            in.nextToken();
//            String s = String.valueOf((int) in.nval);
//            char[] arr = s.toCharArray();
//            int len = arr.length;
//            if ((arr[len - 1] - '0') % 2 == 0) {
//                out.println(s);
//                continue;
//            }
//            int index = -1;
//            for (int i = 0; i < len - 1; i++) {
//                if ((arr[i] - '0') % 2 == 0) {
//                    index = i;
//                    break;
//                }
//            }
//            if (index == -1) {
//                out.println(-1);
//            } else {
//                char temp = arr[index];
//                arr[index] = arr[len - 1];
//                arr[len - 1] = temp;
//                out.println(new String(arr));
//            }
//        }
//        out.close();
//    }


    static List<Integer> path;
    static boolean[] used;
    static int count = 0;

    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(System.out);
        in.nextToken();
        int n = (int) in.nval; // 人数
        int[] nums = new int[n + 1]; // 诉求数组
        for (int i = 1; i <= n; i++) {
            in.nextToken();
            nums[i] = (int) in.nval;
        }
        used = new boolean[n + 1];
        path = new ArrayList<>();
        dfs(n, nums);
        out.println(count);
        out.close();
    }

    private static void dfs(int n, int[] nums) {
        if (path.size() == n) {
            if (isValid(n, nums)) {
                count++;
            }
            return;
        }
        for (int i = 1; i <= n; i++) {
            if (!used[i]) {
                path.add(i);
                used[i] = true;
                dfs(n, nums);

//                回溯
                path.remove(path.size() - 1);
                used[i] = false;
            }
        }
    }

    private static boolean isValid(int n, int[] nums) {
        for (int i = 1; i <= n; i++) {
            int demand = nums[i];
            if (demand == i) {
                continue;
            }
            int posI = path.indexOf(i);
            int posD = path.indexOf(demand);
            if (posI >= posD) {
                return false;
            }
        }
        return true;
    }
}


